<script setup lang="ts" name="SystemSmsLogDetail">
import { DICT_TYPE } from '@/enums/dictEnums'
import { SmsLogType } from '@/api/system/messages/sms/log'
import { SmsChannelApi, SmsChannelType } from '@/api/system/messages/sms/channel'
import { formatDate } from '@/utils/dateUtil'

const { t } = useI18n() // 国际化
const visible = ref(false) // 弹窗的是否展示
const detailLoading = ref(false) // 表单的加载中
const detailData = ref({} as SmsLogType) // 详情数据
const channelList = ref<SmsChannelType[]>([]) // 短信渠道列表

/** 打开弹窗 */
const open = async (data: SmsLogType) => {
  visible.value = true
  // 设置数据
  detailLoading.value = true
  try {
    detailData.value = data
  } finally {
    detailLoading.value = false
  }
  // 加载渠道列表
  channelList.value = await SmsChannelApi.simple()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗
</script>

<template>
  <Dialog v-model="visible" :title="t('common', 'detail')" height="50vh" type="scroll" width="800px">
    <el-descriptions v-if="detailData" :column="1" border class="mr-10px">
      <el-descriptions-item :label="t('common', 'id')">
        {{ detailData.id }}
      </el-descriptions-item>
      <el-descriptions-item :label="t('sys.messages.sms.log', 'channel')">
        {{ channelList.find((channel) => channel.id === detailData.channelId)?.signature }}
        <dict-tag :type="DICT_TYPE.SYSTEM_SMS_CHANNEL_CODE" :value="detailData.channelCode" />
      </el-descriptions-item>
      <el-descriptions-item :label="t('sys.messages.sms.log', 'template')">
        {{ detailData.templateId }} | {{ detailData.templateCode }}
        <dict-tag :type="DICT_TYPE.SYSTEM_SMS_TEMPLATE_TYPE" :value="detailData.templateType" />
      </el-descriptions-item>
      <el-descriptions-item :label="t('sys.messages.sms.log', 'apiTemplateId')">
        {{ detailData.apiTemplateId }}
      </el-descriptions-item>
      <el-descriptions-item :label="t('sys.messages.sms.log', 'userInfo')">
        {{ detailData.mobile }}
        <span v-if="detailData.userType && detailData.userId">
          <dict-tag :type="DICT_TYPE.USER_TYPE" :value="detailData.userType" />
          ({{ detailData.userId }})
        </span>
      </el-descriptions-item>
      <el-descriptions-item :label="t('sys.messages.sms.log', 'templateContent')">
        <div v-html="detailData.templateContent" />
      </el-descriptions-item>
      <el-descriptions-item :label="t('sys.messages.sms.log', 'templateParams')">
        <el-input v-model="detailData.templateParams" :autosize="{ maxRows: 20 }" :readonly="true" type="textarea" />
      </el-descriptions-item>
      <el-descriptions-item :label="t('common', 'createTime')">
        {{ detailData.create_time }}
      </el-descriptions-item>
      <el-descriptions-item :label="t('sys.messages.sms.log', 'sendStatus')">
        <dict-tag :type="DICT_TYPE.SYSTEM_SMS_SEND_STATUS" :value="detailData.sendStatus" />
        {{ formatDate(detailData.sendTime!) }}
      </el-descriptions-item>
      <el-descriptions-item :label="t('sys.messages.sms.log', 'sendResult')"> {{ detailData.apiSendCode }} | {{ detailData.apiSendMsg }} </el-descriptions-item>
      <el-descriptions-item :label="t('sys.messages.sms.log', 'apiSerialNo')">
        {{ detailData.apiSerialNo }}
      </el-descriptions-item>
      <el-descriptions-item :label="t('sys.messages.sms.log', 'apiRequestId')">
        {{ detailData.apiRequestId }}
      </el-descriptions-item>
      <el-descriptions-item :label="t('sys.messages.sms.log', 'receiveStatus')">
        <dict-tag :type="DICT_TYPE.SYSTEM_SMS_RECEIVE_STATUS" :value="detailData.receiveStatus" />
        {{ formatDate(detailData.receiveTime!) }}
      </el-descriptions-item>
      <el-descriptions-item :label="t('sys.messages.sms.log', 'receiveResult')"> {{ detailData.apiReceiveCode }} | {{ detailData.apiReceiveMsg }} </el-descriptions-item>
    </el-descriptions>
  </Dialog>
</template>

<style scoped lang="scss"></style>
